﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LumenWorks.Framework.IO.Csv;
using System.Windows.Forms;
using ZedGraph;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Collections;

namespace PermonAnalizer
{
    class PerfmonLoader
    {

        public PerfmonLoader()
        {
            Trace.Listeners.Add(new TextWriterTraceListener("perfmon_analyser.log"));
            Trace.AutoFlush = true;
            Trace.Indent();
            Trace.WriteLine("Perfmon Loader");

        }

        ZedGraphControl graphPanel;

        public void parseHeader()
        {

        }

        public List<Array> ReadPerfmonLog(ZedGraphControl zgu, List<String> files)
        {
            graphPanel = zgu;

            ArrayList interestingCountersName = new ArrayList();
            interestingCountersName.Add(@"\\\\.+\\Processor.+\\% CPU Time");

            ArrayList interestingCounters = new ArrayList();

            List<DataPoint> interestingData = new List<DataPoint>();
            try
            {
                foreach (String file in files)
                {
                    try
                    {
                        using (CsvReader csv = new CsvReader(new StreamReader(file), true))
                        {
                            int fieldCount = csv.FieldCount;
                            string[] headers = csv.GetFieldHeaders();
                            int validHeaders = 0;

                            //Obtain the amount of valid populated headers on the file
                            for (int i = 0; i < headers.Length; i++)
                            {
                                //System.Diagnostics.Debug.WriteLine( i + headers[i] );
                                if (headers[i].Contains("Column"))
                                {
                                    validHeaders = i;
                                    break;
                                }
                                else
                                {
                                    /*
                                    foreach(String lookForName in interestingCountersName ){
                                    
                                        Match match = Regex.Match( headers[i], lookForName, RegexOptions.IgnoreCase );

                                        if (match.Success)
                                        {
                                            interestingCounters.Add(i);
                                            Trace.WriteLine("INTERESTING HEADER DUMP(" + i + ")" + headers[i]);
                                        }
                                     
                                        interestingCounters.Add(1487);
                                    }*/

                                }
                            }
                            interestingCounters.Add(1487);

                            while (csv.ReadNextRecord())
                            {
                                foreach (int i in interestingCounters)
                                {
                                    try
                                    {
                                        //Time Processing
                                        string strTime = csv[0];
                                        long value = long.Parse(csv[i]);

                                        DataPoint point = new DataPoint( strTime ,value);
                                        interestingData.Add(point);

                                        if (value >= 90)
                                        {
                                            Trace.WriteLine( strTime + " ## HIGH CPU CONDITION DETECTED ## " + csv[i]);
                                        }

                                        Trace.WriteLine( strTime + " Value i:(" + i + ") " + headers[i] + "\t\t\t\t| " + value);

                                    }
                                    catch (LumenWorks.Framework.IO.Csv.MissingFieldCsvException ex)
                                    {
                                        System.Diagnostics.Debug.WriteLine("Value not present");
                                    }
                                }
                                Trace.Flush();
                            }
                        }
                    }
                    catch (System.IO.IOException ex)
                    {
                        MessageBox.Show(ex.Message, "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (Exception ex)
                    {

                    }
                }
                CPUGraph graph = new CPUGraph(zgu, interestingData);
                graph.CreateCPUGraph();
            }
            catch (Exception ex)
            {
            }
            return null;
        }
    }
}
    

